// utils/menu-utils.js

// 扫描所有 views 下的 index.vue 文件
const views = import.meta.glob('@/views/**/index.vue')

export function convertMenusToRoutes(menus) {
  const routes = []

  menus.forEach(menu => {
    if (menu.children?.length) {
      menu.children.forEach(child => {
        const viewPath = `/src/views${child.path.replace('/app', '')}/index.vue`
        const component = views[viewPath]
        if (component) {
          routes.push({
            path: child.path,
            name: child.name,
            component
          })
        } else {
          console.warn(`未找到页面组件：${viewPath}`)
        }
      })
    } else if (menu.path) {
      const viewPath = `/src/views${menu.path.replace('/app', '')}/index.vue`
      const component = views[viewPath]
      if (component) {
        routes.push({
          path: menu.path,
          name: menu.name,
          component
        })
      } else {
        console.warn(`未找到页面组件：${viewPath}`)
      }
    }
  })

  return routes
}